<template>
    <div class="boxs">
        <a-tabs v-model:activeKey="activeKey" type="card">
            <a-tab-pane key="user" tab="客户端">
                <a-tabs v-model:activeKey="changeKey" tab-position="left" animated>
                    <a-tab-pane key="1" tab="隐私协议">
                        <fwb :content="userData.ys" @setData="setys" :sc="usersc" :FWBheight="'1000px'"></fwb>
                    </a-tab-pane>
                    <a-tab-pane key="2" tab="软件协议">
                        <fwb :content="userData.rj" @setData="setrj" :sc="usersc" :FWBheight="'1000px'"></fwb>
                    </a-tab-pane>
                    <a-tab-pane key="3" tab="注销协议">
                        <fwb :content="userData.zx" @setData="setzx" :sc="usersc" :FWBheight="'1000px'"></fwb>
                    </a-tab-pane>
                </a-tabs>

            </a-tab-pane>
            <a-tab-pane key="tech" tab="技师端">
                <a-tabs v-model:activeKey="techKey" tab-position="left" animated>
                    <a-tab-pane key="1" tab="隐私协议">
                        <fwb :content="techData.ys" @setData="setTechys" :sc="usersc" :FWBheight="'1000px'"></fwb>
                    </a-tab-pane>
                    <a-tab-pane key="2" tab="软件协议">
                        <fwb :content="techData.rj" @setData="setTechrj" :sc="usersc" :FWBheight="'1000px'"></fwb>
                    </a-tab-pane>
                    <a-tab-pane key="3" tab="注销协议">
                        <fwb :content="techData.zx" @setData="setTechzx" :sc="usersc" :FWBheight="'1000px'"></fwb>
                    </a-tab-pane>
                </a-tabs>
            </a-tab-pane>

        </a-tabs>
        <div style="display: flex;justify-content: flex-end;">
            <a-popconfirm title="是否上传?" ok-text="Yes" cancel-text="No" @confirm="confirm">
                <a-button type="primary" size="large">上传</a-button>
            </a-popconfirm>
        </div>

    </div>
</template>


<script setup>
import { ref, onMounted, reactive } from 'vue'
import { fwbAPI } from '../../../api/set';
import fwb from '../../../components/fwb.vue'
import { useUserData } from '../../../stores/userData';
import { useRouter } from 'vue-router';
import share from '../../../util/commnet'
import { message } from 'ant-design-vue';



const activeKey = ref('user')
const changeKey = ref('1')
const techKey = ref('1')
const usersc = ref(false);

const router = useRouter();  //路由
const store = useUserData();    //pinia



const setys = (value) => {  //修改用户隐私
    userUPdate.ys = value;
}
const setrj = (value) => {  //修改用户软件
    userUPdate.rj = value;
}
const setzx = (value) => {  //修改用户注销
    userUPdate.zx = value;
}

const setTechys = (value) => {  //修改tech隐私
    techUPdate.ys = value;
}
const setTechrj = (value) => {  //修改tech软件
    techUPdate.rj = value;
}
const setTechzx = (value) => {  //修改tech注销
    techUPdate.zx = value;
}


const userData = reactive({ //用户文件
    ys: '',
    rj: "",
    zx: ""
})
const techData = reactive({ //技师文件
    ys: '',
    rj: "",
    zx: ""
})
const userUPdate = reactive({       //用户上传文件
    ys:"",
    rj:'',
    zx:""
})
const techUPdate = reactive({       //技师上传文件
    ys:"",
    rj:'',
    zx:""
})

const confirm = () => {
    usersc.value = true;
    setTimeout(async()=>{
        if(activeKey.value == 'user'){
            let res = await fwbAPI(`setuser|${userUPdate.ys}|${userUPdate.rj}|${userUPdate.zx}|${store.data.token}|${store.data.uuid}|${store.data.phone}`);
            if (!res || res.code == 406) {                //验证账号状态
                router.replace({ name: 'login' });
                message.error("账号失效", 3)
                return;
            } else if (res.code != 200) {
                message.error(res.message);
                return
            }
            message.success(res.message);
            userData.ys = res.ys;
            userData.rj = res.rj;
            userData.zx = res.zx;
        }else if(activeKey.value == 'tech'){
            let res = await fwbAPI(`settech|${techUPdate.ys}|${techUPdate.rj}|${techUPdate.zx}|${store.data.token}|${store.data.uuid}|${store.data.phone}`);
            if (!res || res.code == 406) {                //验证账号状态
                router.replace({ name: 'login' });
                message.error("账号失效", 3)
                return;
            } else if (res.code != 200) {
                message.error(res.message);
                return
            }
            message.success(res.message);
            techData.ys = res.ys;
            userData.rj = res.rj;
            techData.zx = res.zx;
        }
        
        usersc.value = false;
    },10);
}

onMounted(async () => {
    let res = await fwbAPI(`seluser|${store.data.token}|${store.data.uuid}|${store.data.phone}`);
    let res2 = await fwbAPI(`seltech|${store.data.token}|${store.data.uuid}|${store.data.phone}`);

    console.log(res2);
    if (!res || res.code == 406) {                //验证账号状态
        router.replace({ name: 'login' });
        message.error("账号失效", 3)
        return;
    } else if ((res.code != 200) || (res2.code !=200)) {
        message.error(res.message);
        return
    }
    userData.ys = res.ys;
    userData.rj = res.rj;
    userData.zx = res.zx;
    techData.ys = res2.ys;
    techData.rj = res2.rj;
    techData.zx = res2.zx;

})
</script>

